﻿//======================================================================
//
//        Copyright (C) 2008 HOHO``
//        All rights reserved
//
//        filename :$safeitemrootname$
//        description :文件菜单操作.菜单权限分配设置.以及提取菜单树操作.
//
//        created by 裘中杰 at  $time$
//======================================================================


using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Linq;
using CSM.Models;

namespace CSM.Controllers
{
    public class CSMMenu
    {
        private string lastError;
        private CustomDataContext db = new CustomDataContext();

        public string LastError
        {
            get { return lastError; }
            private set { lastError = value; }
        }
        /// <summary>
        /// 返回 可以显示的并拥有权限的菜单
        /// </summary>
        /// <param name="auths"></param>
        /// <returns></returns>
        public List<TCsm_JS_Menu> GetInAuthsMenus(List<int> auths)
        {
            List<TCsm_JS_Menu> retval;
            var menus = (from m in db.TCsm_JS_Menu
                         //where m.是否显示 == true
                         select m);
            retval = (from m in menus
                      where (m.IsLast==false || auths.Contains(m.MenuID)) && m.IsShow==true
                      orderby m.FatherID
                      select m).ToList();
            return retval;
        }
        private void FillData2TreeView(int menuID, IEnumerable<TCsm_JS_Menu> TotalMenus, ref TreeNode tn)
        {
            var menus = (from m in TotalMenus
                         where m.FatherID == menuID
                         //orderby m.顺序ID descending
                         select m);
            if (menus.Count() == 0)
                return;
            foreach (var menu in menus)
            {
                if (menu.IsLast)
                {
                    //tn.ChildNodes.Add(new TreeNode(@"&nbsp" + menu.MenuName, menu.MenuID.ToString(), @"bullet.jpg", "javascript:top.myTab.Cts('测试1','" + menu.URL + "')", null));
                    tn.ChildNodes.Add(new TreeNode(@"&nbsp" + menu.MenuName, menu.MenuID.ToString(), @"bullet.gif", menu.URL, "main"));
                    //tn.ChildNodes.Add(new TreeNode(@"&nbsp" + menu.MenuName, menu.MenuID.ToString(), @"bullet.gif", "javascript:top.myTab.Cts('" + menu.MenuName + "','" + menu.URL + "')", null));
                }
                else
                {
                    TreeNode childTN = new TreeNode(menu.MenuName);
                    FillData2TreeView(menu.MenuID, TotalMenus, ref childTN);
                    tn.ChildNodes.Add(childTN);

                }

            }

        }
        /// <summary>
        /// 获得当前帐号可以操作的菜单
        /// </summary>
        /// <param name="auths"></param>
        /// <returns></returns>
        public TreeNode GetOwnLeftMenu(List<int> auths)
        {
            var menus = GetInAuthsMenus(auths);
            TreeNode tn = new TreeNode("welcome");
            FillData2TreeView(0, menus, ref tn);
            return tn;
        }



    }
}
